package isnork.g2;

import isnork.sim.Observation;
import isnork.g2.utility.Utility;
import isnork.sim.SeaLifePrototype;
import isnork.sim.GameObject.Direction;
import isnork.sim.iSnorkMessage;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

import javax.swing.text.html.HTMLDocument.Iterator;

public class PiranhaDangerAvoidance implements Strategy {
	
	@Override
	public Map<Direction, Double> process(G2Explorer diver) {
		

		Point2D BOAT = new Point2D.Double(0,0);
		Map<Direction, Double> utility_accumulator = new HashMap<Direction, Double>();
		for (Direction d : Direction.values()) {
			utility_accumulator.put(d, 0.0);
		}
		Double utility = Double.MAX_VALUE;
		
		if ((diver.getHappiness() - diver.suffered) >= diver.expHapp
				* diver.safeDegree) {
			return null;
			// when satisfied, return null
		}
		
		
		if(diver.location.equals(BOAT)){
			if(noDanger(diver)&&(diver.current_round>=diver.justBack+3)){
				utility_accumulator.put(Direction.S, utility);
			}
			else{
				utility_accumulator.put(Direction.STAYPUT, utility);
			}
		}
		else{
			if(noDanger(diver)){
				utility_accumulator.put(Direction.STAYPUT, utility);
			}
			else{

				utility_accumulator.put(Direction.N, utility);
				diver.justBack = diver.current_round;
			}
		}
		return utility_accumulator;
		//System.out.printf("Danger avoidance process started\n");
	}
	static boolean noDanger(G2Explorer diver) {
		boolean flag = true;
		for(Observation obs:diver.whatYouSee){
			if(obs.isDangerous()){
				flag = false;
			}
		}
		return flag;
	}

	static boolean isSafe(G2Explorer diver) {
		Point2D loc,nextloc;
		Direction dir;
		
		if(diver.whatYouSee==null)
			return true;
		
		for(Observation obs:diver.whatYouSee){
			loc = obs.getLocation();
			dir = obs.getDirection();
			nextloc = new Point2D.Double(loc.getX()+dir.getDx(), loc.getY()+dir.getDy());
			if(obs.isDangerous()){
				if(diver.location.distance(nextloc)<=2 || diver.location.distance(loc)<=2)
					return false;
			}
		}
		return true;
	}
	
	static boolean isClear(G2Explorer diver) {
		boolean flag = false;
			Set<iSnorkMessage> messages = diver.incomingMessages;
			for(iSnorkMessage mes:messages){
				if(mes.getSender()==0 && mes.getMsg()!=null &&mes.getMsg().equalsIgnoreCase("a")){
					flag=true;
				}
			}
		
		return flag;
	}

}
